package figures;
/*WBShape.java
 *CS193J Summer 2001
 *  adapt� le 31/08/2009 francois.pfister@ema.fr
 */

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputMethodEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import constants.GlobalDebug;


/** 
 * This class implements an abstract Shape. 
 * Shape class should be subclasses to create 
 * new shapes: circle, rectangles, etc.  
 *
 * @see Component
 */

public class WBShape extends Component implements Cloneable {

  protected int x1, x2, y1, y2;

  protected boolean dSelected = false;

  private static final int dSelectedBoxSize = 4;

  private Color dXORColor = Color.black;
  private boolean dUseXor = false;

  public String asString(){
      String result=x1+";"+x2+";"+y1+";"+y2;
      result+=";"+(int)(getLocation().getX());
      result+=";"+(int)(getLocation().getY());
      if (dSelected)
	     result+=";sel";
	  else	      
	     result+=";notsel";
      return result;
  }
  
  public void parse(String from){
      String[] attrs = new String[20];
      attrs = from.split(";");
      x1 = Integer.parseInt(attrs[0]);
      x2 = Integer.parseInt(attrs[1]);
      y1 = Integer.parseInt(attrs[2]);
      y2 = Integer.parseInt(attrs[3]);
      setLocation(new Point(Integer.parseInt(attrs[4]),Integer.parseInt(attrs[5])));
      setSelectState(attrs[6].equals("sel"));
      init(x1,y1,x2,y2);
  }
  
  
  public WBShape() {
    super();
  }

  
  public void init (int x1, int y1, int x2, int y2) {
      if (GlobalDebug.isOn) 
        System.out.println ("WBShape.init()");
      setCoordinates (x1,y1,x2,y2);
      enableEvents(AWTEvent.MOUSE_EVENT_MASK);
      setXOROff ();
    }  
  
public WBShape (int x1, int y1, int x2, int y2) {
    super ();
    init(x1,y1,x2,y2);
  }

  public void setCoordinates (int x1, int y1, int x2, int y2) {
    setSize (Math.abs(x1-x2), Math.abs (y1-y2));

    if (((x1 > x2) && (y1 > y2)) ||
	((x1 > x2) && (y1 <= y2))) {
      // make sure this.x1 < this.x2
      this.x1 = x2; this.y1 = y2; this.x2 = x1; this.y2 = y1;
    }
    else {
      this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
    }
  }

  public Object clone () {
    System.err.println ("Shape.clone(): Internal Error: This method should not be getting called.  All shapes should override it.");
    return new WBShape (x1,y1,x2,y2);
  }

  public void setSelectState (boolean selected) {
    dSelected = selected;
  }

  public void toggleSelectState () {
    dSelected = !dSelected;
  }

  public boolean getSelectState () {
    return dSelected;
  }

  public void setXOROn () {
    dXORColor = Color.white;
    dUseXor = true;
  }

  public void setXOROff () {
    dUseXor = false;
  }


  public void paint (Graphics g) {
    
    g.setColor (Color.black);

    if (dUseXor) 
      g.setXORMode (dXORColor);

    super.paint (g);
    
    if (dSelected) {
      g.fillRect (0,0,dSelectedBoxSize,dSelectedBoxSize);
      g.fillRect (0,getHeight()-1-dSelectedBoxSize,dSelectedBoxSize,dSelectedBoxSize);
      g.fillRect (getWidth()-1-dSelectedBoxSize,0,dSelectedBoxSize,dSelectedBoxSize);
      g.fillRect (getWidth()-1-dSelectedBoxSize,
		  getHeight()-1-dSelectedBoxSize,
		  dSelectedBoxSize,
		  dSelectedBoxSize);
    }
  }

  public Dimension getPreferredSize () {
    return new Dimension (Math.abs(x1-x2), Math.abs (y1-y2));
  }

  public Dimension getMinimumSize () {
    return new Dimension (Math.abs(x1-x2), Math.abs (y1-y2));
  }

  public Dimension getMaximumSize () {
    return new Dimension (Math.abs(x1-x2), Math.abs (y1-y2));
  }

  public  AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent
				  newEvent) {
    return super.coalesceEvents (existingEvent,newEvent);
  }

  public  void firePropertyChange(String propertyName, Object oldValue,
				  Object newValue) {
    super.firePropertyChange (propertyName, oldValue, newValue);
  }

  public  String paramString() {
    return super.paramString();
  }

  public void processComponentEvent(ComponentEvent e) {
    super.processComponentEvent (e);
  }
  
  public  void processEvent(AWTEvent e) {
    super.processEvent (e);
  }

  public  void processFocusEvent(FocusEvent e) {
    super.processFocusEvent (e);
  }

  public  void processInputMethodEvent(InputMethodEvent e) {
    super.processInputMethodEvent(e);
  }

  public  void processKeyEvent(KeyEvent e) {
    super.processKeyEvent (e);
  }

  public void processMouseEvent(MouseEvent e) {
    super.processMouseEvent (e);
  }

  public  void processMouseMotionEvent(MouseEvent e) {
    super.processMouseMotionEvent(e);
  }
  
}
